package com.erp.bus.Controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.bus.pojo.Inport;
import com.erp.bus.service.GoodsService;
import com.erp.bus.service.ProviderService;
import com.erp.bus.comm.DataAndView;
import com.erp.bus.comm.ResultCode;
import com.erp.bus.pojo.Goods;
import com.erp.bus.pojo.Provider;
import com.erp.bus.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ProviderService providerService;

    @RequestMapping("loadAllGoods")
    public DataAndView loadAllGoods(GoodsVo goodsVo){
        IPage<Goods> page =  new Page<>(goodsVo.getPage(),goodsVo.getLimit());


        QueryWrapper<Goods> qw = new QueryWrapper<>();
        //判断条件是否为空
        if(goodsVo.getProviderid()!=null && goodsVo.getProviderid()!=0){
            qw.like("providerid",goodsVo.getProviderid());
        }
        if(goodsVo.getGoodsname()!=null){
            qw.like("goodsname",goodsVo.getGoodsname());
        }
        if(goodsVo.getProductcode()!=null){
            qw.like("productcode",goodsVo.getProductcode());
        }
        if(goodsVo.getPromitcode()!=null){
            qw.like("promitcode",goodsVo.getPromitcode());
        }
        if(goodsVo.getDescription()!=null){
            qw.like("description",goodsVo.getDescription());
        }
        if(goodsVo.getSize()!=null){
            qw.like("size",goodsVo.getSize());
        }
        //条件查询
        goodsService.page(page,qw);


        // 查询结果
        List<Goods> list = page.getRecords();
        List<Goods> list2 = new ArrayList<>();

        System.out.println(list);
        for (Goods good : list){

            Integer pid = good.getProviderid();
            Provider p = providerService.getById(pid);
            good.setProvidername(p.getProvidername());
            list2.add(good);
        }

        return new DataAndView(list2);
    }

    // 删除   http://localhost:8080/goods/deleteGoods
    @RequestMapping("deleteGoods")
    public ResultCode  deleteGoods(Goods goods){

        // 删除图片


        // 获取用户填写的数据
        ;try{
            boolean bool = goodsService.removeById(goods);

            System.out.println(bool);
            if(bool){
                return ResultCode.SUCCESS_DELETE;
            }else{
                return  ResultCode.FAIL_DELETE;
            }
        }catch (Exception e){

            return  ResultCode.FAIL_DELETE;
        }
    }

    // 添加  http://localhost:8080/goods/addGoods
    @RequestMapping("addGoods")
    public ResultCode addGoods(Goods goods){
        // 防止更新后  添加  id有值
        goods.setId(null);
        ;try{
            boolean bool = goodsService.save(goods);
            if(bool){
                return ResultCode.SUCCESS_ADD;
            }else{
                return ResultCode.FAIL_ADD;
            }
        }catch (Exception e){

            return  ResultCode.FAIL_ADD;
        }
    }

    // 更新  http://localhost:8080/goods/updateGoods
    @RequestMapping("updateGoods")
    public ResultCode updateGoods(Goods goods){
        ;try{
            boolean bool = goodsService.updateById(goods);
            if(bool){
                return ResultCode.SUCCESS_UPDATE;
            }else{
                return ResultCode.FAIL_UPDATE;
            }
        }catch (Exception e){

            return  ResultCode.FAIL_ADD;
        }
    }

    // 查询所有商品
    //http://localhost:8080/goods/loadAllGoodsForSelect
    @RequestMapping("loadAllGoodsForSelect")
    public DataAndView loadAllGoodsForSelect(){

        //查询你所有商品   privodeName  null
        List<Goods> list  = goodsService.list();

        List<Goods> list2 = new ArrayList<>();
        for(Goods good:list){
            // 获取当前商品的供货商id
            Integer provideId = good.getProviderid();
            //通过供货商id 查对应的供货商
            Provider provider = providerService.getById(provideId);
            // 设置当前的商品的供货商名称
            good.setProvidername(provider.getProvidername());
            list2.add(good);
        }


        return new DataAndView(list2);
    }

    //
    //http://localhost:8080/goods/loadGoodsByProviderId?providerid=2
    @RequestMapping("loadGoodsByProviderId")
    public DataAndView loadGoodsByProviderId(Integer providerid){
        //通过providerid 查所有商品
        QueryWrapper<Goods> qw = new QueryWrapper<>();
        if(providerid != 0){
            qw.eq("providerid",providerid);
        }

        List<Goods> lists = goodsService.list(qw);
        List<Goods> list2 = new ArrayList<>();
        for(Goods good:lists){
            Provider provider = providerService.getById(providerid);
            // 设置当前的商品的供货商名称
            good.setProvidername(provider.getProvidername());
            list2.add(good);
        }

        return  new DataAndView(lists);
    }


}
